package com.dfbz.service.impl;

import com.dfbz.dao.UserDao;
import com.dfbz.dao.UserFocusDao;
import com.dfbz.dao.UserInfoDao;
import com.dfbz.model.User;
import com.dfbz.model.UserFocus;
import com.dfbz.model.UserInfo;
import com.dfbz.service.UserService;
import com.dfbz.utils.MailUtil;
import com.dfbz.utils.StrUtil;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDao();
    private UserInfoDao userInfoDao=new UserInfoDao();
    private UserFocusDao userFocusDao=new UserFocusDao();
    @Override
    public User findByName(String username) {
        List<User> list = userDao.findByCol("username", username);
        if (list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public User findByEmail(String email) {
        List<User> emailList = userDao.findByCol("email", email);
        if (emailList.isEmpty()){
            return null;
        }else {
            return emailList.get(0);
        }
    }

    @Override
    public boolean checkByEmailOrName(String email, String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("email", email);
        List<User> userList = userDao.findByCols("or", map);
        return userList.isEmpty();
    }

    @Override
    public boolean addUser(User user) {
        if (userDao.save(user)>0){
            Map<String, Object> map = userDao.getBaseInfoByName(user.getUsername());
            //添加用户info 表信息
            UserInfo info = new UserInfo();
            info.setUserId(Integer.parseInt(map.get("user_id").toString()));
            info.setRealName("雪碧");
            info.setRegisterTime(new Date());
            return userInfoDao.save(info)>0;
        }
        return false;

    }

    @Override
    public String sendEmailCode(String email) {
        String code = StrUtil.getRandomCode(4);
        //用工具类，实现真的发送验证码给用户邮箱
        String str="《小标交友》重置密码";
        String content="你的验证码是："+code+"，两分钟之内有效。";
        try {
            MailUtil.send(email,str,content,MailUtil.UNICODE);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return code;
    }

    @Override
    public boolean resetPassword(String email, String password) {
        List<User> users = userDao.findByCol("email", email);
        if (!users.isEmpty()){
            User user = users.get(0);
            user.setPassword(password);
            return userDao.update(user)>0;
        }

        return false;
    }

    @Override
    public Map<String, Object> findUserBaseInfo(String username) {
        return userDao.getBaseInfoByName(username);
    }

    @Override
    public Map<String, Object> getUserInfo(String id) {
        return userDao.getUserInfoById(id);
    }

    @Override
    public void updateLoginTime(Integer userId) {
        UserInfo info= userInfoDao.findById(userId);
        if (null!=info){
            info.setLoginTime(new Date());
            userInfoDao.update(info);
        }
    }

    @Override
    public List<Map<String, Object>> getUserList(String userId,int currentPage, String name) {
        return userDao.findUsers(userId,currentPage,name);
    }

    @Override
    public long getUserCount(String name) {
        return userDao.getUserTotal(name);
    }

    @Override
    public boolean followUser(String userId, String ufId) {

        UserFocus focus = new UserFocus();
        focus.setUserId(Integer.parseInt(userId));
        focus.setUserFocusId(Integer.parseInt(ufId));

        return userFocusDao.save(focus)>0;
    }

    @Override
    public UserFocus getFollowUser(String userId, String ufId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("user_focus_id",ufId);
        List<UserFocus> focusList = userFocusDao.findByCols("and", map);
        if (!focusList.isEmpty()){
            return focusList.get(0);
        }

        return null;
    }

    @Override
    public boolean unFollowUser(Integer id) {

        return userFocusDao.deleteById(id)>0;
    }

    @Override
    public void uploadImg(String userId,String path) {
        UserInfo byId = userInfoDao.findById(Integer.parseInt(userId));
        byId.setPic(path);
        userInfoDao.update(byId);
    }


}